class tableDemoClass {
  // 构造函数
  constructor() {
    this.getTableData = this.getTableData.bind(this);
    this.ColmergeData = this.ColmergeData.bind(this);
    this.setHighLight = this.setHighLight.bind(this);
    this.setHighLights = this.setHighLights.bind(this);
    this.getFactor = this.getFactor.bind(this);
    this.addFactor = this.addFactor.bind(this);
    this.setFactorTitle = this.setFactorTitle.bind(this);
    this.deleteFactor = this.deleteFactor.bind(this);
    this.getRule = this.getRule.bind(this);
    this.findRule = this.findRule.bind(this);
    this.setRuleValues = this.setRuleValues.bind(this);
    this.addRule = this.addRule.bind(this);
    this.deleteRule = this.deleteRule.bind(this);
    this.deleteRuleByCondition = this.deleteRuleByCondition.bind(this);
    this.clear = this.clear.bind(this);
    this.reset = this.reset.bind(this);
    this.getRuleResult = this.getRuleResult.bind(this);
    this.findRuleResult = this.findRuleResult.bind(this);
    this.setRuleCondition = this.setRuleCondition.bind(this);
    this.setRuleResult = this.setRuleResult.bind(this);
    this.setRuleData = this.setRuleData.bind(this);
    this.getResult = this.getResult.bind(this);
    this.setResultTitle = this.setResultTitle.bind(this);
    this.addResult = this.addResult.bind(this);
    this.deleteResult = this.deleteResult.bind(this);
    this.setHeaderHighLight = this.setHeaderHighLight.bind(this);
  }
  init(example) {
    console.log('tableDemo', example.$refs);
    this._tableDemo = example.$refs.ruleTable;
    this.parentExample = example
  }
  // 获取表格数据
  getTableData() {
    return this._tableDemo.data;
  }
  // // 设置表格数据
  // setTableData(data) {
  //   this._tableDemo.generaData(data);
  // }
  // 设置高亮-单个设置
  setHighLight({ index, name }) {
    this._tableDemo.heilight = [{ index, name }]
    this._tableDemo.$forceUpdate();
  }
  // 设置高亮-多个设置
  setHighLights(heilight) {
    this._tableDemo.heilight = heilight;
    this._tableDemo.$forceUpdate();
  }
  // 设置高亮-头部
  setHeaderHighLight(heilight) {
    this._tableDemo.heardHelight = [...heilight];
    // this._tableDemo.$forceUpdate();
  }
  // 获取因子数据
  getFactor(code) {
    if (!code) {
      return this._tableDemo.selfColFactors
    }
    return this._tableDemo.selfColFactors.find(item => item.code === code);
  }
  // 添加因子
  addFactor({ code, title, dataType, render, parser, options } = {}) {
    this._tableDemo.selfColFactors = [...this._tableDemo.selfColFactors, { code, title, dataType, render, parser, options }];
  }
  // 修改因子
  setFactorTitle(code, title) {
    this._tableDemo.selfColFactors = this._tableDemo.selfColFactors.map(item => {
      return {
        ...item,
        title: item.code === code ? title : item.title
      }
    });
  }
  // 删除因子
  deleteFactor(code) {
    this._tableDemo.selfColFactors = this._tableDemo.selfColFactors.filter(item => item.code !== code);
  }

  // 通过key获取rule
  getRule(key) {
    return key.map(item => this._tableDemo.selfRules.find(rule => rule.key === item))
  }

  // 根据条件查询规则
  findRule(condition, options = {}) {
    const { mode = 'first' } = options
    const keys = Object.keys(condition);
    if (mode === 'first') {
      return this._tableDemo.selfRules.find(rule => {
        return keys.every(key => {
          return (rule.factors[key] === condition[key]) || (rule.results[key] === condition[key])
        })
      })
    } else if (mode === 'all') {
      return this._tableDemo.selfRules.filter(rule => {
        return keys.every(key => (rule.factors[key] === condition[key]) || (rule.results[key] === condition[key]))
      })
    }
  }

  // 获取规则结果
  getRuleResult(key) {
    return this._tableDemo.selfRules.find(rule => rule.key === key).results;
  }

  // 根据条件查询规则结果
  findRuleResult(condition, options = {}) {
    const { mode = 'first' } = options
    const keys = Object.keys(condition);
    if (mode === 'first') {
      return this._tableDemo.selfRules.find(rule => {
        return keys.every(key => {
          return (rule.factors[key] === condition[key]) || (rule.results[key] === condition[key])
        })
      }).results
    }
    else if (mode === 'all') {
      return this._tableDemo.selfRules.filter(rule => {
        return keys.every(key => (rule.factors[key] === condition[key]) || (rule.results[key] === condition[key]))
      }).map(item => item.results)
    }
  }

  // 设置规则条件
  setRuleCondition(key, conditionData) {
    this._tableDemo.selfRules = this._tableDemo.selfRules.map(rule => {
      if (rule.key === key) {
        return {
          ...rule,
          factors: conditionData
        }
      } else {
        return rule;
      }
    })
  }

  // 设置规则结果
  setRuleResult(key, resultData) {
    this._tableDemo.selfRules = this._tableDemo.selfRules.map(rule => {
      if (rule.key === key) {
        return {
          ...rule,
          results: resultData
        }
      }
      else {
        return rule;
      }
    })
  }

  // 设置规则数据
  setRuleData(key, data) {
    this._tableDemo.selfRules = this._tableDemo.selfRules.map(rule => {
      if (rule.key === key) {
        return {
          ...rule,
          ...data
        }
      }
      else {
        return rule;
      }
    })
  }


  // 修改规则
  setRuleValues(key, values) {
    this._tableDemo.selfRules = this._tableDemo.selfRules.map(rule => {
      if (rule.key === key) {
        return {
          ...rule,
          results: values
        }
      } else {
        return rule;
      }
    })
  }

  // 添加规则
  addRule(rule) {
    this._tableDemo.generaData([...this._tableDemo.data, rule ?? {}]);
  }

  // 删除规则
  deleteRule(key) {
    console.log(this._tableDemo.selfRules);
    const _key = this._tableDemo.keyname
    this._tableDemo.selfRules = this._tableDemo.selfRules.filter(item => item[_key] != key);
  }

  // 根据条件删除规则
  deleteRuleByCondition(condition) {
    const keys = Object.keys(condition);
    this._tableDemo.selfRules = this._tableDemo.selfRules.filter(rule => {
      return keys.every(key => (rule.factors[key] === condition[key]) || (rule.results[key] === condition[key]))
    })
  }
  // 重置规则表
  reset() {
    // 销毁组件
    this.parentExample.resetTable();
  }

  // 清空所有规则result数据
  clear() {
    this._tableDemo.selfRules = this._tableDemo.selfRules.map(rule => {
      return {
        ...rule,
        results: {}
      }
    })
  }
  // 获取结果
  getResult(code) {
    return this._tableDemo.selfResult.find(item => item.code === code);
  }
  // 设置结果
  setResultTitle(code, title) {
    this._tableDemo.selfResult.find(item => item.code === code).title = title;
  }
  // 添加结果
  addResult({ code, title, dataType, render, parser, options } = {}) {
    this._tableDemo.selfResult = [...this._tableDemo.selfResult, { code, title, dataType, render, parser, options }];
  }
  // 删除结果
  deleteResult(result) {
    this._tableDemo.selfResult = this._tableDemo.selfResult.filter(item => item !== result);
  }
  // 设置表格
  ColmergeData({ columns, ColIndex, tableKeys }) {
    return this._tableDemo.ColmergeData({ columns, ColIndex, tableKeys });
    // console.log('tableDemoClass', this._tableDemo);
  }
}

const RuleTable = {
  createRuleTable: () => new tableDemoClass()
}

export default RuleTable;